<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;

/**
 * 系统菜单分组控制器
 * @package App\Http\Controllers\Admin
 */
class SysMenuGroupController extends BaseController
{
    private $menuSer;

    public function __construct()
    {
        parent::__construct();
        $this->menuSer = new \App\Services\SysMenuService();
    }

    /**
     * 系统菜单分组管理
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\SysMenuGroup::select('*');
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('group_name', 'like', '%'.$keyword.'%');
            }
            return $query->paginate($pageSize);
        }
        return view('admin.sysmenugroup.index', $this->viewData);
    }

    /**
     * 新建分组
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function create(\App\Http\Requests\AdminSysMenuGroupPost $request)
    {
        if ($request->ajax()){
            if($this->menuSer->saveGroup($request)){
                return $this->showMsg('success', '新增成功', 'admin.sysmenugroup.index');
            }else{
                return $this->showMsg('error');
            }
        }
    }
    /**
     * 编辑
     * @param Request $request
     * @param $id
     */
    public function update(\App\Http\Requests\AdminSysMenuGroupPost $request)
    {
        if($this->menuSer->saveGroup($request)){
            return $this->showMsg('success', '编辑成功', 'admin.sysmenugroup.index');
        }else{
            return $this->showMsg('error', '编辑失败');
        }
    }
    /**
     * 删除
     * @param Request $request
     */
    public function destroy(Request $request)
    {
        if ($request->ajax()){
            if($this->menuSer->delGroup($request->input('group_id', 0))){
                return $this->showMsg('success', '删除成功', 'admin.sysmenugroup.index');
            }else{
                return $this->showMsg('error', '删除失败');
            }
        }
    }
}
